package 分类.二叉树;

public class 最大二叉树 {
    public TreeNode constructMaximumBinaryTree(int[] nums) {
        return build(nums,0,nums.length-1);
    }

    TreeNode build(int[] nums,int lo,int hi){
        if(lo>hi){
            return null;
        }
        int index=-1;
        int maxVal=Integer.MIN_VALUE;
        for (int i=lo;i<=hi;i++){
            if(maxVal<nums[i]){
                index=i;
                maxVal=nums[i];
            }
        }
        TreeNode root=new TreeNode(maxVal);
        root.left=build(nums,lo,index-1);
        root.right=build(nums,index+1,hi);
        return root;
    }
}
